package com.siblings.loan.ui.tab.impl

import android.content.Context
import com.lib.base.base.BaseFragment
import com.siblings.loan.R
import com.siblings.loan.app.Apps
import com.siblings.loan.ui.loan.fm.SibLoanSafetyFm
import com.siblings.loan.ui.loan.fm.SibLoanLayerFm
import com.siblings.loan.ui.loan.fm.SibLoanOrderFm
import com.siblings.loan.ui.tab.view.ISibMainView
import com.siblings.mvp.presenter.MvpPresenter
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/4 15:46
 * @Annotation：主页面 Presenter 实现
 */
class SibMainPresenterImpl(context: Context?) : MvpPresenter<ISibMainView>(context) {
    //val 是不可变的 var 可变的
    private val tabTitleArray = arrayListOf(
        Apps.app.getString(R.string.tab_home),
        Apps.app.getString(R.string.tab_repay),
        Apps.app.getString(R.string.tab_date)
    )
    private val tabImageNormal = arrayListOf(
        R.mipmap.pic_tab_home_normal,
        R.mipmap.pic_tab_repay_normal,
        R.mipmap.pic_tab_date_normal
    )

    private val tabImageSelect = arrayListOf(
        R.mipmap.pic_tab_home_select,
        R.mipmap.pic_tab_repay_select,
        R.mipmap.pic_tab_date_select
    )

    //定义tab的每个页面
    private val tabList = arrayListOf<JSONObject>()
    private val fragments = ArrayList<BaseFragment>()
    private var curtIndex = 0

    /**
     * 加载Tabbar数据
     */
    fun loadSibTabList(){
        //tab
        for (i in 0 until tabTitleArray.size) {
            val tabObj = JSONObject()
            tabObj.put("tabId", i)
            tabObj.put("tabTitle", tabTitleArray[i])
            tabObj.put("tabImageSelect", tabImageSelect[i])
            tabObj.put("tabImageNormal", tabImageNormal[i])
            tabObj.put("isSelect", false)
            tabList.add(tabObj)
        }
        mvpView.showSibTabList(tabList)
    }

    /**
     * 加载FragmentList
     */
    fun loadSibFragmentList(){
        fragments.add(SibLoanLayerFm())//贷款中间层
        fragments.add(SibLoanOrderFm())//待还款
        fragments.add(SibLoanSafetyFm())//数据安全

        //默认选中第1个
        setSibCurrentItemIndex(0)
    }

    /**
     * 切换Tab
     */
    fun setSibCurrentItemIndex(index: Int) {
        curtIndex = index
        for (tabObj in tabList) {
            when (tabObj.optInt("tabId")) {
                index -> {
                    tabObj.put("isSelect", true)
                }
                else -> {
                    tabObj.put("isSelect", false)
                }
            }
        }
        mvpView.notifyTabDataChanged()

        val fragment = fragments[curtIndex]
        mvpView.showSibFragment(fragment,curtIndex)
    }
}